
package com.compuvest.web;

// <editor-fold defaultstate="collapsed" desc="import">
import java.io.Serializable;
import java.io.UnsupportedEncodingException;

import java.util.Map;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
// </editor-fold>

public class PreferenceUtil implements Serializable {

// <editor-fold defaultstate="collapsed" desc="init">
    private static final int PREF_SIZE = 7;

    private StringBuilder hiddenTags;   // for price range
    private StringBuilder links;        // for url links
    private Map<Integer,String> content;
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="PreferenceUtil()">

    public PreferenceUtil() {
        this(null, 0);
    }

    public PreferenceUtil(HttpServletRequest request, int subcatID) {

        if(subcatID == 0) return;

        hiddenTags = new StringBuilder();
        links = new StringBuilder();
        content = new HashMap<Integer,String>(PREF_SIZE);

        String pref;
        for(int position = 0; position < PREF_SIZE; position++) {

            pref = request.getParameter("p" + position);
            if(pref == null || pref.isEmpty()) continue;

            try {
                content.put(position, pref);
                hiddenTags.append("<input type='hidden' name='p").append(position).append("' value='").append(pref).append("'/>");
                links.append("&p").append(position).append('=').append(java.net.URLEncoder.encode(pref, "UTF-8"));
            }
            catch(UnsupportedEncodingException ex) {
                Logger.getLogger(PreferenceUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getHiddenTags()">

    public String getHiddenTags() {
        return hiddenTags == null ? "" : hiddenTags.toString();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getLink()">

    public String getLink() {
        return links == null ? "" : links.toString();
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getContent()">

    public Map<Integer,String> getContent() {
        return content;
    }
// </editor-fold>

}
